1585A - Life of a Flower - CodeForces Solution


implementation *800

Please click on ads to support us..

Python Code:

t=int(input())

for i in range(t):
    n=int(input())
    l=list(map(int,input().split()))
    count=0
    if l[0]==0:
        count=1
    else:
        count+=2
    for i in range(1,len(l)):
        if l[i]==0 and l[i-1]==0:
            print(-1)
            break
        elif l[i]==1 and l[i-1]==1:
            count+=5
        elif l[i]==1 and l[i-1]==0:
            count+=1
        elif l[i]==0 and l[i-1]==1:
            continue
    else:
        print(count)

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define N 1000005
#define  sz(a)  (int) a.size() 
// #define IOS ios::sync_with_stdio(0)

using namespace std;

void solve()
{
    // IOS;

}
int32_t main() {
    int Tst = 1;
    cin>>Tst;
    while(Tst--)
    {
        int n;
        cin>>n;
        int v[100+5];
        int cn=0,f=0;
        for (int i = 0; i < n; i++) {
            cin>>v[i];
        }
        for(int i=0;i<n;i++)
        {
            if(v[i]==0)cn++;
            else cn=0;
            if(cn>=2)f=1;
        }
        if(n==1)
        {
            cout<<v[0]+1<<'\n';
            continue;
        }
        if(f==1)
        {
            cout<<"-1"<<'\n';
            continue;
        }
        int l=1;
        l+=v[0];
        for (int i = 1; i < n; i++) {
            if(v[i]+v[i-1]==2)
            {
                l+=5;
            }
            else{
                if(v[i]!=0) l++;
            }
        }

        cout<<l<<'\n';
    }
    //solve();
}


Comments

Submit
0 Comments
More Questions

787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self